extends Camera2D

# 缩放相关变量
var min_zoom = Vector2(0.5, 0.5)  # 最小缩放比例（0.5倍）
var max_zoom = Vector2(1.0, 1.0)  # 最大缩放比例（1倍）
var zoom_speed = 0.1  # 缩放速度
var target_zoom = Vector2(1.0, 1.0)  # 目标缩放值
var zoom_smoothness = 5.0  # 缩放平滑度

# 移动相关变量
var move_speed = 500.0  # 移动速度（像素/秒）
var move_direction = Vector2.ZERO  # 移动方向
var camera_bounds = Rect2()  # 相机移动边界
var has_bounds = false  # 是否有边界限制

# 键盘控制
var zoom_in_key = KEY_EQUAL  # + 键放大
var zoom_out_key = KEY_MINUS  # - 键缩小
var reset_zoom_key = KEY_0  # 0 键重置缩放

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	# 初始化相机：将相机放置到地图中心，从1.0平滑缩小到0.5
	# 设置相机边界 - 使用set_bounds_from_map而不是set_camera_bounds
	set_bounds_from_map(Vector2(1152*2, 648*2))
	# 将相机定位到地图中心，确保缩放围绕中心进行
	if has_bounds:
		var center: Vector2 = camera_bounds.position + camera_bounds.size * 0.5
		position = center
	# 启动缩放从1.0到0.5（平滑）
	zoom = max_zoom
	target_zoom = min_zoom
	
	# 启用相机处理
	process_mode = Node.PROCESS_MODE_INHERIT

# 处理输入事件
func _input(event: InputEvent) -> void:
	# 键盘控制缩放
	if event is InputEventKey and event.pressed:
		if event.keycode == zoom_in_key:  # 放大
			target_zoom = Vector2(min(target_zoom.x + zoom_speed, max_zoom.x), 
								  min(target_zoom.y + zoom_speed, max_zoom.y))
		elif event.keycode == zoom_out_key:  # 缩小
			target_zoom = Vector2(max(target_zoom.x - zoom_speed, min_zoom.x), 
								  max(target_zoom.y - zoom_speed, min_zoom.y))
		elif event.keycode == reset_zoom_key:  # 重置为0.5倍
			target_zoom = min_zoom

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	# 处理移动输入
	move_direction = Vector2.ZERO
	if Input.is_key_pressed(KEY_W):
		move_direction.y -= 1
	if Input.is_key_pressed(KEY_S):
		move_direction.y += 1
	if Input.is_key_pressed(KEY_A):
		move_direction.x -= 1
	if Input.is_key_pressed(KEY_D):
		move_direction.x += 1
	
	# 归一化移动方向
	if move_direction.length() > 0:
		move_direction = move_direction.normalized()
	
	# 应用移动
	var move_delta = move_direction * move_speed * delta
	position += move_delta
	
	# 边界检查（如果有边界限制）
	if has_bounds:
		# 计算当前视口大小（考虑缩放）
		var viewport_size = get_viewport_rect().size / zoom
		var half_viewport = viewport_size / 2
		
		# 限制相机位置在边界内
		position.x = clamp(position.x, camera_bounds.position.x + half_viewport.x, 
						  camera_bounds.position.x + camera_bounds.size.x - half_viewport.x)
		position.y = clamp(position.y, camera_bounds.position.y + half_viewport.y, 
						  camera_bounds.position.y + camera_bounds.size.y - half_viewport.y)
	
	# 平滑缩放
	zoom = zoom.lerp(target_zoom, delta * zoom_smoothness)
	
# 设置缩放到指定值的函数
func set_zoom_level(level: float) -> void:
	var new_zoom = Vector2(level, level)
	# 确保缩放值在允许范围内
	new_zoom.x = clamp(new_zoom.x, min_zoom.x, max_zoom.x)
	new_zoom.y = clamp(new_zoom.y, min_zoom.y, max_zoom.y)
	target_zoom = new_zoom
	
# 设置相机移动边界
func set_camera_bounds(bounds: Rect2) -> void:
	camera_bounds = bounds
	has_bounds = true
	
# 根据地图大小自动设置边界
func set_bounds_from_map(map_size: Vector2) -> void:
	camera_bounds = Rect2(Vector2.ZERO, map_size)
	has_bounds = true
